package com.sec.samsung.android.app.baymax.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sec.samsung.android.app.baymax.R;
import com.sec.samsung.android.app.baymax.model.user.Account;
import com.sec.samsung.android.app.baymax.utils.BaymaxConstant;

public class BodyPartChooser extends Activity implements OnClickListener {
    //===========================Constant Define=============
    private static final int NUMBER_BODY_MAN_FRONT = 8;
    private static final int NUMBER_BODY_MAN_BACK = 7;
    private static final int NUMBER_BODY_WOMAN_FRONT = 8;
    private static final int NUMBER_BODY_WOMAN_BACK = 7;

    private static final int BODY_MAN_FRONT = 0;
    private static final int BODY_MAN_BACK = 1;
    private static final int BODY_WOMAN_FRONT = 2;
    private static final int BODY_WOMAN_BACK = 3;

    private static final int PART_HEAD = 1;
    private static final int PART_NECK = 2;
    private static final int PART_CHEST = 3;
    private static final int PART_HAND = 4;
    private static final int PART_ABDOMEN = 5;
    private static final int PART_BUTT = 6;
    private static final int PART_LEG = 7;

    private static final String PART_STRING_HEAD = "head";
    private static final String PART_STRING_NECK = "neck";
    private static final String PART_STRING_CHEST = "chest";
    private static final String PART_STRING_HAND = "hand";
    private static final String PART_STRING_ABDOMEN = "abdomen";
    private static final String PART_STRING_BUTT = "butt";
    private static final String PART_STRING_LEG = "leg";

    private static final int STICK_TIME = 300;
    //==========================Class Define================

    //==========================Control Define==============
    private ImageView mImgBody;
    private ImageView mImgFrontBack;
    private LinearLayout mBtnNext;
    private Button mBtnBack;
    //==========================Variable Define=============
    private Account mUser;
    private static int[] arrBitmapEffectRes;
    private int countEffect = 0;
    private int bodyType = 0;
    private int bodyWidth, bodyHeight;
    private float xTouch, yTouch;
    private float scaleX = 1, scaleY = 1;
    private int bodyPartChoose = 0;
    private String bodyPartString = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.body_part_chooser);

        initLayout();
    }

    private void initLayout() {
        mBtnBack = (Button) findViewById(R.id.btn_back);
        mImgBody = (ImageView) findViewById(R.id.img_body);
        mImgFrontBack = (ImageView) findViewById(R.id.btn_back_front_view);
        mBtnNext = (LinearLayout) findViewById(R.id.layout_next);
        mBtnNext.setOnClickListener(this);
        mImgFrontBack.setOnClickListener(this);
        mBtnBack.setOnClickListener(this);

        String userData = getIntent().getExtras().getString("user_data");
        Gson gson = new GsonBuilder().serializeNulls().create();
        mUser = gson.fromJson(userData, Account.class);
        if (mUser == null)
            bodyType = BODY_MAN_FRONT;
        else if (mUser.getGender().equals(BaymaxConstant.MALE))
            bodyType = BODY_MAN_FRONT;
        else if (mUser.getGender().equals(BaymaxConstant.FEMALE))
            bodyType = BODY_WOMAN_FRONT;
        startEffectForBody(bodyType);

        Toast.makeText(this, getString(R.string.choose_body_part_pained), Toast.LENGTH_SHORT).show();
        mImgBody.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                bodyWidth = mImgBody.getWidth();
                bodyHeight = mImgBody.getHeight();
                scaleX = bodyWidth / 228.f;
                scaleY = bodyHeight / 315.f;
                xTouch = event.getX();
                yTouch = event.getY();
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if ((xTouch > 95 * scaleX && xTouch < 130 * scaleX) & (yTouch > 15 * scaleY && yTouch < 55 * scaleY)) {
                        // touch on head
                        bodyPartChoose = PART_HEAD;
                        bodyPartString = PART_STRING_HEAD;
                    } else if ((xTouch > 90 * scaleX && xTouch < 135 * scaleX) & (yTouch > 55 * scaleY && yTouch < 65 * scaleY)) {
                        // touch on neck
                        bodyPartChoose = PART_NECK;
                        bodyPartString = PART_STRING_NECK;
                    } else if ((xTouch > 85 * scaleX && xTouch < 135 * scaleX) & (yTouch > 65 * scaleY && yTouch < 100 * scaleY)) {
                        // touch on chest
                        bodyPartChoose = PART_CHEST;
                        bodyPartString = PART_STRING_CHEST;
                    } else if ((xTouch > 35 * scaleX && xTouch < 85 * scaleX) & (yTouch > 65 * scaleY && yTouch < 160 * scaleY)) {
                        // touch on left hand
                        bodyPartChoose = PART_HAND;
                        bodyPartString = PART_STRING_HAND;
                    } else if ((xTouch > 135 * scaleX && xTouch < 190 * scaleX) & (yTouch > 65 * scaleY && yTouch < 160 * scaleY)) {
                        //touch on right hand 
                        bodyPartChoose = PART_HAND;
                        bodyPartString = PART_STRING_HAND;
                    } else if ((xTouch > 90 * scaleX && xTouch < 130 * scaleX) & (yTouch > 100 * scaleY && yTouch < 130 * scaleY)) {
                        //touch on abdomen
                        bodyPartChoose = PART_ABDOMEN;
                        bodyPartString = PART_STRING_ABDOMEN;
                    } else if ((xTouch > 85 * scaleX && xTouch < 135 * scaleX) & (yTouch > 130 * scaleY && yTouch < 150 * scaleY)) {
                        //touch on butt
                        bodyPartChoose = PART_BUTT;
                        bodyPartString = PART_STRING_BUTT;
                    } else if ((xTouch > 65 * scaleX && xTouch < 110 * scaleX) & (yTouch > 150 * scaleY && yTouch < 295 * scaleY)) {
                        //touch left leg
                        bodyPartChoose = PART_LEG;
                        bodyPartString = PART_STRING_LEG;
                    } else if ((xTouch > 115 * scaleX && xTouch < 155 * scaleX) & (yTouch > 150 * scaleY && yTouch < 295 * scaleY)) {
                        //touch right leg
                        bodyPartChoose = PART_LEG;
                        bodyPartString = PART_STRING_LEG;
                    }
                    chooseBodyPart(bodyPartChoose);
                    break;
                case MotionEvent.ACTION_UP:

                    break;
                case MotionEvent.ACTION_MOVE:

                    break;
                default:
                    break;
                }
                return true;
            }
        });
    }

    public void chooseBodyPart(int part) {
        if (part == 0)
            return;
        mImgBody.setBackgroundResource(arrBitmapEffectRes[part]);
        Toast.makeText(this, " You have chosen part of " + bodyPartString, Toast.LENGTH_SHORT).show();
    }

    private void startEffectForBody(int bodyType) {
        countEffect = 0;
        switch (bodyType) {
        case BODY_MAN_FRONT:
            startEffectForManFront();
            break;
        case BODY_MAN_BACK:
            startEffectForManBack();
            break;
        case BODY_WOMAN_FRONT:
            startEffectForWomanFront();
            break;
        case BODY_WOMAN_BACK:
            startEffectForWomanBack();
            break;
        default:
            break;
        }

    }

    private void startEffectForWomanBack() {
        // read data animation resource ID
        arrBitmapEffectRes = new int[NUMBER_BODY_WOMAN_BACK];
        for (int i = 0; i < arrBitmapEffectRes.length; i++) {
            arrBitmapEffectRes[i] = getResources().getIdentifier(
                    "drawable/woman_back_" + i, null, getPackageName());
        }
        new CountDownTimer(STICK_TIME * (NUMBER_BODY_WOMAN_BACK + 1), STICK_TIME) {

            @Override
            public void onTick(long millisUntilFinished) {
                mImgBody.setBackgroundResource(arrBitmapEffectRes[countEffect]);
                countEffect = ++countEffect % NUMBER_BODY_WOMAN_BACK;
            }

            @Override
            public void onFinish() {
                mImgBody.setBackgroundResource(arrBitmapEffectRes[0]);
                countEffect = 0;
            }
        }.start();

    }

    private void startEffectForWomanFront() {
        // read data animation resource ID
        arrBitmapEffectRes = new int[NUMBER_BODY_WOMAN_FRONT];
        for (int i = 0; i < arrBitmapEffectRes.length; i++) {
            arrBitmapEffectRes[i] = getResources().getIdentifier(
                    "drawable/woman_front_" + i, null, getPackageName());
        }
        new CountDownTimer(STICK_TIME * (NUMBER_BODY_WOMAN_FRONT + 1), STICK_TIME) {

            @Override
            public void onTick(long millisUntilFinished) {
                mImgBody.setBackgroundResource(arrBitmapEffectRes[countEffect]);
                countEffect = ++countEffect % NUMBER_BODY_WOMAN_FRONT;
            }

            @Override
            public void onFinish() {
                mImgBody.setBackgroundResource(arrBitmapEffectRes[0]);
                countEffect = 0;
            }
        }.start();

    }

    private void startEffectForManBack() {
        // read data animation resource ID
        arrBitmapEffectRes = new int[NUMBER_BODY_MAN_BACK];
        for (int i = 0; i < arrBitmapEffectRes.length; i++) {
            arrBitmapEffectRes[i] = getResources().getIdentifier(
                    "drawable/man_back_" + i, null, getPackageName());
        }
        new CountDownTimer(STICK_TIME * (NUMBER_BODY_MAN_BACK + 1), STICK_TIME) {

            @Override
            public void onTick(long millisUntilFinished) {
                mImgBody.setBackgroundResource(arrBitmapEffectRes[countEffect]);
                countEffect = ++countEffect % NUMBER_BODY_MAN_BACK;
            }

            @Override
            public void onFinish() {
                mImgBody.setBackgroundResource(arrBitmapEffectRes[0]);
                countEffect = 0;
            }
        }.start();

    }

    private void startEffectForManFront() {
        // read data animation resource ID
        arrBitmapEffectRes = new int[NUMBER_BODY_MAN_FRONT];
        for (int i = 0; i < arrBitmapEffectRes.length; i++) {
            arrBitmapEffectRes[i] = getResources().getIdentifier(
                    "drawable/man_front_" + i, null, getPackageName());
        }
        new CountDownTimer(STICK_TIME * (NUMBER_BODY_MAN_FRONT + 1), STICK_TIME) {

            @Override
            public void onTick(long millisUntilFinished) {
                mImgBody.setBackgroundResource(arrBitmapEffectRes[countEffect]);
                countEffect = ++countEffect % NUMBER_BODY_MAN_FRONT;
            }

            @Override
            public void onFinish() {
                mImgBody.setBackgroundResource(arrBitmapEffectRes[0]);
                countEffect = 0;
            }
        }.start();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_back_front_view:
            changeAnimationBody();
            break;
        case R.id.layout_next:
            onClickBtnNext();
            break;
        case R.id.btn_back:
            finish();
            break;
        default:
            break;
        }
    }

    private void onClickBtnNext() {
        if (bodyPartChoose == 0) {
            Toast.makeText(this, " Please choose pained body part", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent(BodyPartChooser.this, BaseSymptomQuestion.class);
        intent.putExtra("body_part", bodyPartString);
        startActivity(intent);
    }

    private void changeAnimationBody() {
        switch (bodyType) {
        case BODY_MAN_BACK:
            bodyType = BODY_MAN_FRONT;
            break;
        case BODY_MAN_FRONT:
            bodyType = BODY_MAN_BACK;
            break;
        case BODY_WOMAN_BACK:
            bodyType = BODY_WOMAN_FRONT;
            break;
        case BODY_WOMAN_FRONT:
            bodyType = BODY_WOMAN_BACK;
            break;
        default:
            break;
        }
        startEffectForBody(bodyType);
    }

}
